<template>
	<div class="ele-body">
		<el-card shadow="never">
			<!-- 搜索表单 -->
			<el-form :model="table.where" label-width="77px" class="ele-form-search" @keyup.enter.native="$refs.table.reload()" @submit.native.prevent>
				<el-row :gutter="15">
					<el-col :md="6" :sm="12">
						<el-form-item label="任务名称:"><el-input v-model="table.where.jobname" placeholder="请输入" clearable /></el-form-item>
					</el-col>
					<el-col :md="6" :sm="12">
						<el-form-item label="任务标识:"><el-input v-model="table.where.jobmethod" placeholder="请输入" clearable /></el-form-item>
					</el-col>
					<el-col :md="6" :sm="12">
						<el-form-item label="运行状态:">
							<el-select v-model="table.where.state" placeholder="请选择" clearable class="ele-fluid">
								<el-option label="启动" value="0" />
								<el-option label="停止" value="1" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :md="6" :sm="12">
						<div class="ele-form-actions">
							<el-button type="primary" @click="$refs.table.reload()" icon="el-icon-search" class="ele-btn-icon">查询</el-button>
							<el-button @click="(table.where = {}) && $refs.table.reload()">重置</el-button>
						</div>
					</el-col>
				</el-row>
			</el-form>
			<!-- 操作按钮 -->
			<div class="ele-table-tool ele-table-tool-default">
				<el-button @click="showEdit = true" type="primary" icon="el-icon-plus" class="ele-btn-icon">添加</el-button>
				<el-button @click="remove()" type="danger" icon="el-icon-delete" class="ele-btn-icon">删除</el-button>
				<el-button @click="$router.push('/scm/joblog')" type="primary" icon="el-icon-time" class="ele-btn-icon">日志</el-button>
				<!-- <el-button @click="help()" type="primary" icon="el-icon-help" class="el-icon-question">帮助/el-button> -->
				<el-tooltip placement="top">
					<div slot="content">
						<a href="https://www.bejson.com/othertools/cron/" target="_blank" style="color: #DD4A68;">Cron表达式生成器</a>
						<br />
						<br />
						每隔5秒执行一次：*/5 * * * * ?
						<br />
						每隔1分钟执行一次：0 */1 * * * ?
						<br />
						每天23点执行一次：0 0 23 * * ?
						<br />
						每天凌晨1点执行一次：0 0 1 * * ?
						<br />
						每月1号凌晨1点执行一次：0 0 1 1 * ?
						<br />
						每月最后一天23点执行一次：0 0 23 L * ?
						<br />
						每周星期天凌晨1点实行一次：0 0 1 ? * L
						<br />
						在26分、29分、33分执行一次：0 26,29,33 * * * ?
						<br />
						每天的8点、12点、17点都执行一次：0 0 8,12,17 * * ?
						<br />
						每天的7点到21点都执行一次：0 0 7-21 * * ?
						<br />
						<br />
					</div>
					<el-button type="info" icon="el-icon-question">帮助</el-button>
				</el-tooltip>
			</div>
			<!-- 数据表格 -->
			<ele-data-table ref="table" :config="table" :choose.sync="choose" highlight-current-row>
				<template slot-scope="{ index }">
					<el-table-column type="selection" width="45" align="center" fixed="left" />
					<el-table-column type="index" :index="index" width="45" align="center" fixed="left" show-overflow-tooltip />
					<el-table-column prop="jobname" label="任务名称" sortable="custom" show-overflow-tooltip min-width="150" />
					<el-table-column prop="jobcorn" label="执行表达式" sortable="custom" show-overflow-tooltip min-width="150" />
					<el-table-column prop="state" label="运行状态" width="95px" sortable="custom" :resizable="false">
						<template slot-scope="{ row }">
							<el-switch v-model="row.state" @change="editState(row)" :active-value="0" :inactive-value="1" />
						</template>
					</el-table-column>
					<el-table-column prop="jobmethod" label="任务标识" sortable="custom" show-overflow-tooltip min-width="150" />
					<el-table-column prop="jobparm" label="任务参数" sortable="custom" show-overflow-tooltip min-width="150" />
					<el-table-column prop="remark" label="备注" sortable="custom" show-overflow-tooltip min-width="110" />
					<!-- <el-table-column prop="createtime" label="创建时间" sortable="custom" show-overflow-tooltip min-width="110">
						<template slot-scope="{ row }">
							{{ row.createtime | toDateString }}
						</template>
					</el-table-column> -->
					<el-table-column label="操作" width="200px" align="center" :resizable="false">
						<template slot-scope="{ row }">
							<el-link @click="edit(row)" icon="el-icon-edit" type="primary" :underline="false">修改</el-link>
							<el-popconfirm title="确定要删除此任务吗？" @confirm="remove(row)" class="ele-action">
								<el-link slot="reference" icon="el-icon-delete" type="danger" :underline="false">删除</el-link>
							</el-popconfirm>
							<el-popconfirm title="确定要执行1次此任务吗？" @confirm="runOneTask(row)" class="ele-action">
								<el-link slot="reference" icon="el-icon-timer" type="warning" :underline="false">执行</el-link>
							</el-popconfirm>
						</template>
					</el-table-column>
				</template>
			</ele-data-table>
		</el-card>
		<!-- 编辑弹窗 -->
		<el-dialog
			:title="editForm.jobid ? '修改任务' : '添加任务'"
			:visible.sync="showEdit"
			width="450px"
			@closed="editForm = {}"
			:destroy-on-close="true"
			:lock-scroll="false"
			:close-on-click-modal="false"
		>
			<el-form :model="editForm" ref="editForm" :rules="editRules" label-width="100px">
				<el-form-item label="任务名称:" prop="jobname"><el-input v-model="editForm.jobname" placeholder="请输入任务名称" clearable /></el-form-item>
				<el-form-item label="执行表达式:" prop="jobcorn"><el-input v-model="editForm.jobcorn" placeholder="请输入执行表达式" clearable /></el-form-item>
				<el-form-item label="任务标识:" prop="jobmethod">
					<el-input v-model="editForm.jobmethod" placeholder="请输入任务标识" clearable :rows="2" type="textarea" />
				</el-form-item>
				<el-form-item label="任务参数:"><el-input v-model="editForm.jobparm" placeholder="请输入任务参数" clearable :rows="2" type="textarea" /></el-form-item>
				<el-form-item label="备注:"><el-input v-model="editForm.remark" placeholder="请输入备注" :rows="2" type="textarea" /></el-form-item>
			</el-form>
			<div slot="footer">
				<el-button @click="showEdit = false">取消</el-button>
				<el-button type="primary" @click="save">保存</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
export default {
	name: 'ScmJob',
	data() {
		return {
			table: { url: '/scm/job/page', where: {} }, // 表格配置
			choose: [], // 表格选中数据
			showEdit: false, // 是否显示表单弹窗
			editForm: {}, // 表单数据
			editRules: {
				// 表单验证规则
				jobname: [{ required: true, message: '请输入任务名称', trigger: 'blur' }],
				jobmethod: [{ required: true, message: '请输入任务标识', trigger: 'blur' }],
				jobcorn: [{ required: true, message: '请输入任务表达式', trigger: 'blur' }]
			}
		};
	},
	computed: {},
	methods: {
		/* 显示编辑 */
		edit(row) {
			this.editForm = Object.assign({}, row);
			this.showEdit = true;
		},
		/* 保存编辑 */
		save() {
			this.$refs['editForm'].validate(valid => {
				if (valid) {
					const loading = this.$loading({ lock: true });
					const posturl = this.editForm.jobid ? '/scm/job/update' : '/scm/job/add';
					this.$http
						.post(posturl, this.editForm)
						.then(res => {
							loading.close();
							if (res.data.code === 0) {
								this.showEdit = false;
								this.$message({ type: 'success', message: res.data.msg });
								this.$refs.table.reload();
							} else {
								this.$message.error(res.data.msg);
							}
						})
						.catch(e => {
							loading.close();
							this.$message.error(e.message);
						});
				} else {
					return false;
				}
			});
		},
		/* 手动执行一次*/
		runOneTask(row){
			const loading = this.$loading({ lock: true });
			this.$http
				.post('/scm/job/runOneTask', row) //
				.then(res => {
					loading.close();
					if (res.data.code === 0) {
						this.$message({ type: 'success', message: res.data.msg });
						this.$refs.table.reload();
					} else {
						this.$message.error(res.data.msg);
					}
				})
				.catch(e => {
					loading.close();
					this.$message.error(e.message);
				});
		},
		/* 删除 */
		remove(row) {
			if (!row) {
				// 批量删除
				if (this.choose.length === 0) return this.$message.error('请至少选择一条数据');
				let ids = this.choose.map(d => d.jobid);
				this.$confirm('确定要删除选中的任务吗?', '提示', { type: 'warning' })
					.then(() => {
						const loading = this.$loading({ lock: true });
						this.$http
							.post('/scm/job/removeBatch', { ids: ids })
							.then(res => {
								loading.close();
								if (res.data.code === 0) {
									this.$message({ type: 'success', message: res.data.msg });
									this.$refs.table.reload();
								} else {
									this.$message.error(res.data.msg);
								}
							})
							.catch(e => {
								loading.close();
								this.$message.error(e.message);
							});
					})
					.catch(() => 0);
			} else {
				// 单个删除
				const loading = this.$loading({ lock: true });
				this.$http
					.post('/scm/job/remove?id=' + row.jobid) //
					.then(res => {
						loading.close();
						if (res.data.code === 0) {
							this.$message({ type: 'success', message: res.data.msg });
							this.$refs.table.reload();
						} else {
							this.$message.error(res.data.msg);
						}
					})
					.catch(e => {
						loading.close();
						this.$message.error(e.message);
					});
			}
		},
		/* 更改状态 */
		editState(row) {
			const loading = this.$loading({ lock: true });
			var urlStr = '/scm/job';
			if (row.state == 0) {
				urlStr = '/scm/job/addTask';
			} else if (row.state == 1) {
				urlStr = '/scm/job/removeTask';
			}
			this.$http
				.post(urlStr, row)
				.then(res => {
					loading.close();
					if (res.data.code === 0) {
						this.$message({ type: 'success', message: res.data.msg });
					} else {
						row.state = !row.state ? 1 : 0;
						this.$message.error(res.data.msg);
					}
					//row.schedulerid = res.data.data.schedulerid;
				})
				.catch(e => {
					loading.close();
					this.$message.error(e.message);
				});
		}
	}
};
</script>

<style scoped></style>
